//
//  LoginResetSecretController.m
//  HaiGeEr_Student
//
//  Created by liuchen on 2018/9/10.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "LoginResetSecretController.h"
#import "LoginResetSecretView.h"


@interface LoginResetSecretController ()
@property (nonatomic,weak) LoginResetSecretView *loginResetSecretView;
@property (nonatomic,weak) UIButton *skipBtn;
@property (nonatomic,weak) UIButton *sureBtn;
@end

@implementation LoginResetSecretController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.view.backgroundColor = [UIColor whiteColor];
    //跳过按钮
    UIButton *skipBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    skipBtn.frame = CGRectMake(650*SCALEUNIT, SYSTEM_STATUSBAR_HEGHT, 100*SCALEUNIT, 83*SCALEUNIT);
    [skipBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [skipBtn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-2]];
    [skipBtn setTitleColor:RGBA(78, 78, 78, 1) forState:UIControlStateNormal];
    [skipBtn addTarget:self action:@selector(jumpToMainPage) forControlEvents:UIControlEventTouchUpInside];
    _skipBtn = skipBtn;
    [self.view addSubview:self.skipBtn];
    //重置密码界面
    LoginResetSecretView *loginResetSecretView = [[LoginResetSecretView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.skipBtn.frame), [UIScreen mainScreen].bounds.size.width, (self.resetJudge?430:535)*SCALEUNIT) WithJudge:self.resetJudge];
    _loginResetSecretView = loginResetSecretView;
    [self.view addSubview:self.loginResetSecretView];
    //确定按钮
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sureBtn.frame = CGRectMake(70*SCALEUNIT, CGRectGetMaxY(self.loginResetSecretView.frame)+85*SCALEUNIT, 610*SCALEUNIT, 90*SCALEUNIT);
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont]];
    [sureBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [sureBtn setBackgroundColor:LOGINBUTTONBACKRGBA];
    [sureBtn.layer setShadowColor:[UIColor blackColor].CGColor];
    [sureBtn.layer setShadowOffset:CGSizeMake(5*SCALEUNIT, 5*SCALEUNIT)];
    [sureBtn.layer setShadowOpacity:0.8];
    [sureBtn addTarget:self action:@selector(sureChange:) forControlEvents:UIControlEventTouchUpInside];
    _sureBtn = sureBtn;
    [self.view addSubview:self.sureBtn];
}

#pragma mark ==========确认==========
- (void)sureChange:(UIButton *)btn{
    if (!self.resetJudge) {
        if (isEmpty(self.loginResetSecretView.secretWithOldView.textField.text)) {
            [self showMessage:@"请输入旧密码"];
            return;
        }
    }
    if (isEmpty(self.loginResetSecretView.secretWithNewView.textField.text)) {
        [self showMessage:@"请输入新密码"];
    }else if ([self.loginResetSecretView.secretWithNewView.textField.text length]<6 || [self.loginResetSecretView.secretWithNewView.textField.text length]>20) {
        [self showMessage:@"密码格式错误"];
    }else if (isEmpty(self.loginResetSecretView.sureSecretWithNewView.textField.text)) {
        [self showMessage:@"请再次输入新密码"];
    }else if (![self.loginResetSecretView.sureSecretWithNewView.textField .text isEqualToString:self.loginResetSecretView.secretWithNewView.textField.text]) {
        [self showMessage:@"两次密码输入不一致"];
    }else {
        [self showLoading];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setValue:self.phone forKey:@"phone"];
        [dic setValue:BASE64([self.phone substringFromIndex:5]) forKey:@"old"];
        [dic setValue:BASE64(self.loginResetSecretView.secretWithNewView.textField.text) forKey:@"new"];
        [[NetworkEngine sharedClient] GET:LOGINCHANGESECRETBYFIRST data:dic success:^(id responseObject) {
            [self showSuccess:[responseObject objectForKey:@"msg"]];
            if ([[responseObject objectForKey:@"code"] integerValue] == 1) {
                [UserMessage sharedInstance].model = [UserModel mj_objectWithKeyValues:[responseObject objectForKey:@"data"]];
                [[NSUserDefaults standardUserDefaults] setObject:[responseObject objectForKey:@"data"] forKey:@"userMessage"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self performSelector:@selector(jumpToMainPage) withObject:nil afterDelay:1.5f];
            } else {[self showMessage:[responseObject objectForKey:@"msg"]];}
        } failure:^(NSString *error) {
            [self hideHUD];
        }];
    }
}

-(void)jumpToMainPage{
    [[UIApplication sharedApplication].delegate window].rootViewController = [CoachTabBarController shareLZHTabbarController];
    [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
